export function UUid() {
  const T = function T() {
    const d = 1 * new Date()
    let i = 0
    while (d == 1 * new Date()) {
      i++
    }
    return d.toString(16) + i.toString(16)
  }
  const R = function R() {
    return Math.random().toString(16).replace('.', '')
  }
  const UA = function (n) {
    const ua = navigator.userAgent
    let i, ch, buffer = []
    let ret = 0

    function xor(result, byte_array) {
      let j, tmp = 0
      for (j = 0; j < byte_array.length; j++) {
        tmp |= (buffer[j] << j * 8)
      }
      return result ^ tmp
    }

    for (i = 0; i < ua.length; i++) {
      ch = ua.charCodeAt(i)
      buffer.unshift(ch & 0xFF)
      if (buffer.length >= 4) {
        ret = xor(ret, buffer)
        buffer = []
      }
    }

    if (buffer.length > 0) {
      ret = xor(ret, buffer)
    }

    return ret.toString(16)
  }

  return function createUUId() {
    let se = String(window.screen.height * window.screen.width)
    if (se && /\d{5,}/.test(se)) {
      se = se.toString(16)
    } else {
      se = String(Math.random() * 31242).replace('.', '').slice(0, 8)
    }
    // const val = (T() + '-' + R() + '-' + UA() + '-' + se + '-' + T())
    const val = `${T()}-${R()}-${UA()}-${se}-${T()}`
    if (val) {
      return val
    }
    return (String(Math.random()) + String(Math.random()) + String(Math.random())).slice(2, 15)
  }
}

export function createSessionId() {
  const arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
  'R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i',
  'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','r',
  '0','1','2','3','4','5','6','7','8','9']
  let str = ''
  for (let i = 0; i < 6; i++) {
    str += arr[Math.floor(Math.random() * arr.length)]
  }
  return `${Date.now()}-${str}`
}